package com.xiangxiao.rpan.file.controller;

import com.xiangxiao.rpan.file.constant.GlobalConsts;
import com.xiangxiao.rpan.file.constant.ResponseCode;
import com.xiangxiao.rpan.file.constant.ResponseMessage;
import com.xiangxiao.rpan.file.consumer.IConsumerSettingsClient;
import com.xiangxiao.rpan.file.dto.JsonObject;
import com.xiangxiao.rpan.file.dto.LinkSettingDto;
import com.xiangxiao.rpan.file.dto.QueryShortDto;
import com.xiangxiao.rpan.file.entity.ShortLink;
import com.xiangxiao.rpan.file.entity.UpdateViewSetting;
import com.xiangxiao.rpan.file.service.ShortLinkManagerService;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @auther xiangxiao
 * @email 573768011@qq.com
 * @data 2023/11/24 11:23
 */
@Slf4j
@RestController
@RequestMapping("/admin")
public class ShortLinkManagerController {
  private static Logger logger = LoggerFactory.getLogger(ShortLinkManagerController.class);
  @Value("${spring.redis.timeout}")
  private int timeout;
  @Resource
  private StringRedisTemplate stringRedisTemplate;
  @Resource
  private ShortLinkManagerService shortLinkManagerService;
  @Resource
  private IConsumerSettingsClient consumerSettingsClient;

  @GetMapping("/link/list")
  public JsonObject queryLinkList(QueryShortDto queryShortDto) {
    String redisKey = GlobalConsts.RPAN_FILE  + GlobalConsts.SHORT_LINK_COUNT;
    int total = 0;
    try {
      if (StringUtils.isNotBlank(stringRedisTemplate.opsForValue().get(redisKey))) {
        total = Integer.parseInt(stringRedisTemplate.opsForValue().get(redisKey));
      } else {
        int offset = (queryShortDto.getPage() - 1) * queryShortDto.getLimit();
        queryShortDto.setOffset(offset);
        total = shortLinkManagerService.queryAllLinkCount(queryShortDto);
      };
      List<ShortLink> links = shortLinkManagerService.queryLinByPagination(queryShortDto);
      int maxPage = (total - 1) / queryShortDto.getLimit() + 1;

      // 获取成功,存入redis,在redis存储未过期之前则直接从redis取用
      stringRedisTemplate.opsForValue().set(redisKey, JSON.toJSONString(total), timeout, TimeUnit.MILLISECONDS);
      return new JsonObject(ResponseMessage.QUERY_SHORT_LINK_SUECCESS, ResponseCode.OK, "", links, total, maxPage);
    } catch (Exception e) {
      logger.error(ResponseMessage.QUERY_SHORT_LINK_FAILED, e);
      return new JsonObject(ResponseMessage.QUERY_SHORT_LINK_FAILED, ResponseCode.QUERY_SHORT_LINK_FAILED);
    }
  }

  @PutMapping("/config/link")
  public JsonObject updateLinkSetting(@Valid @RequestBody LinkSettingDto linkSettingDto) {
    try {
      UpdateViewSetting settings = new UpdateViewSetting();
      BeanUtils.copyProperties(linkSettingDto, settings);
      consumerSettingsClient.updateViewSetting(settings);
      return new JsonObject(ResponseMessage.MODIFY_SHORT_LINK_SUECCESS, ResponseCode.OK, true);
    } catch (Exception e) {
      logger.error(ResponseMessage.MODIFY_SHORT_LINK_FAILED, e);
      return new JsonObject(ResponseMessage.MODIFY_SHORT_LINK_FAILED, ResponseCode.MODIFY_SHORT_LINK_FAILED);
    }
  }
}
